package view;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.util.LinkedList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import model.Grizz;
import model.Mapa;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.TrueTypeFont;

public class Grizzly extends BasicGame {  
    

    private static AppGameContainer app;
    private static Graphics g, background;
    private Mapa mapa;
    private LinkedList<Grizz> agentes;

    public Grizzly() {
        super("Grizzly");


    }

    public static void main(String[] args) {
        try {

            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            app = new AppGameContainer(new Grizzly());
            app.setDisplayMode(d.width, d.height, false);
            app.setAlwaysRender(false);
            app.setFullscreen(true);
            app.setShowFPS(false);
            app.setTargetFrameRate(100);
            app.setTitle("Grizzly");
            app.setIcon("charIcon.png");
            app.start();


        } catch (SlickException e) {
            System.out.println(e);
        }
    }

    @Override
    public void init(GameContainer gc) throws SlickException {
        g = new Graphics();
        background = new Graphics();
        this.mapa = new Mapa();
        this.agentes = new LinkedList<>();
        ExecutorService executor = Executors.newFixedThreadPool(80);

        for (int i = 0; i < this.mapa.getWIDTH(); i++) {
            Grizz a = new Grizz(true, i, 0);
            executor.execute(a);
            this.agentes.add(a);
            Grizz b = new Grizz(false, i, 29);
            executor.execute(b);
            this.agentes.add(b);

        }
    }

    @Override
    public void update(GameContainer gc, int i) throws SlickException {
        //render(gc, g);
    }

    @Override
    public void render(GameContainer gc, Graphics grphcs) throws SlickException {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int deslocamento = d.width - mapa.getWIDTH() * mapa.getTILE_HEIGHT();
        mapa.getMapa().render(deslocamento, 0);
        for (Grizz agente : this.agentes) {
            agente.getSprite().draw(deslocamento + agente.getPosX() * mapa.getTILE_HEIGHT(), agente.getPosY() * mapa.getTILE_HEIGHT());
        }

        Font font = new Font("Arial", Font.BOLD, 32);
        TrueTypeFont ttf = new TrueTypeFont(font, true);

        ttf.drawString(10, 10, "Pontuação Branco: ");
        ttf.drawString(10, 50, "" + Mapa.getPBranco());
        ttf.drawString(10, 90, " Restantes: " + Mapa.getnBranco());

        ttf.drawString(10, (int) d.getHeight() - 150, "Pontuação Preto: ");
        ttf.drawString(10, (int) d.getHeight() - 110, "" + Mapa.getPPreto());
        ttf.drawString(10, (int) d.getHeight() - 70, " Restantes: " + Mapa.getnPreto());

        grphcs.setLineWidth(5);
        grphcs.drawRect(0, 0, deslocamento - 10, d.height - 10);
        


    }
}
